/**
 * creation date: 14-may-08
 */

package org.jfonia.connect;


import java.util.List;

/**
 * Specialization of ListElement<Integer> useful for slice-keys: 
 * gives Integer.MIN_VALUE or Integer.MAX_VALUE when index is before first resp. after last element.
 *  
 * @author wijnand.schepens@gmail.com
 *
 */

public class SafeListElement extends ListElement<Integer> 
{
	public SafeListElement(List<Integer> list, Value<Integer> index) {
		super(list, index);
	}

	@Override
	public Integer get() {
		int i = super.index.get();
		if (i < 0)
			return Integer.MIN_VALUE;
		else if (i < super.list.size())
			return super.list.get(i);
		else
			return Integer.MAX_VALUE;
	}

	@Override
	public void set(Integer newValue) {
		int i = super.index.get();
		if (i < 0)
			throw new RuntimeException("Cannot change element at index " + i);
		else if (i < super.list.size())
			super.list.set(i, newValue);
		else
			throw new RuntimeException("Cannot change element at index " + i);
	}

}
